/**
 * @author 徐楠
 * @date 2022/4/17 21:57
 * @version 1.0
 */

package com.xunan.likou;

public class NumberOfWordsInString {
    public static void main(String[] args) {
        //String s = "Hello, my name is John";
        //String s = "love live! mu'sic forever";
        //String s = "                ";
        String s = ", , , ,        a, eaefa";
        //String s = "The one-hour drama series Westworld is a dark odyssey about the dawn of artificial consciousness and the evolution of sin. Set at the intersection of the near future and the reimagined past, it explores a world in which every human appetite, no matter how noble or depraved, can be indulged.";

        int i = countSegments(s);
        System.out.println(i);
    }

    public static int countSegments(String s) {
        int length = s.length();
        if (s.isEmpty()) {
            return 0;
        }
        int count = 0;
        if (Character.isLetter(s.charAt(0))) {
            System.out.println("1");
            count++;
        }
        int temp = 1;
        for (int i = 0; i < length - 1; i++) {
            if (s.charAt(i) == '\'' ||s.charAt(i) == '-'  && Character.isLetter(s.charAt(i + 1))) {
                System.out.println("2");
                continue;
            }
            if (!Character.isLetter(s.charAt(i)) && !Character.isLetter(s.charAt(i + 1))) {
                System.out.println("3");
                continue;
            }
            if (!Character.isLetter(s.charAt(i)) && Character.isLetter(s.charAt(i + 1))) {
                System.out.println("4  "+ temp++);
                count++;
            }
        }
        return count;
    }

    public static int countSegmentsPlus(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if ((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
                count++;
            }
        }
        return count;
    }

}
